const query = require("../db/mysql")
const { success, error } = require("../module/module")

class Home {
    //获取商品数据
    getaddres = async (ctx) => {
        let sql = 'select * from homeAddress where 1=1 '
        let data = await query(sql)
        ctx.body = data
    }
    //换页
    changePage = async (ctx) => {
        let { pageIndex, pageNum, state } = ctx.request.body
        //先判断是销售分页还是租赁分页
        if (state) {
            let sql = `select * from homeAddress where 1=1 `
            let countSql = 'select count(id) as total from homeAddress '
            let total = await query(countSql)
            if (pageIndex * 1) {
                sql += `limit ${(pageIndex * 1 - 1) * pageNum * 1},${pageNum * 1} `
            }
            let data = await query(sql)
            ctx.body = {
                total: total[0].total,
                data
            }
        } else {
            let sql = `select * from RentalHousing where 1=1 `
            let countSql = 'select count(id) as total from RentalHousing '
            let total = await query(countSql)
            if (pageIndex * 1) {
                sql += `limit ${(pageIndex * 1 - 1) * pageNum * 1},${pageNum * 1} `
            }
            let data = await query(sql)
            ctx.body = {
                total: total[0].total,
                data
            }
        }

    }
    //删除销售
    deleteAdd = async (ctx) => {
        let { id } = ctx.request.body
        let sql = `delete  from homeAddress where 1=1 and id=${id} `
        let data = await query(sql)
        if (data.affectedRows === 1) {
            ctx.body = success(data, '删除成功')
        } else {
            ctx.body = error('删除失败')
        }
    }
    //更新查找
    find = async (ctx) => {
        let { id } = ctx.request.body
        let sql = `select * from homeAddress where 1=1 `
        if (id) {
            sql += `and id=${id} `
        }
        let data = await query(sql)
        ctx.body = success(data)
    }
    //更新
    updataAddre = async (ctx) => {
        let { id, buildName, address, price, time, sort, state } = ctx.request.body
        let sql = `update homeAddress set buildName='${buildName}' , address='${address}',price='${price}',time='${time}' ,sort='${sort}', state='${state}' where id=${id} `

        let data = await query(sql)
        if (data.affectedRows === 1) {
            ctx.body = success(data, '更新成功')
        } else {
            ctx.body = error('更新失败')
        }
    }
    //新增销售
    addNewdreass = async (ctx) => {
        let { buildName, address, price, time, sort, state } = ctx.request.body
        let homeSql = `select * from homeAddress where 1=1  and address like "%${address}%" and buildName like "%${buildName}%"`
        let isHave = await query(homeSql)
        if (isHave.length) {
            ctx.body = error('已存在此房源')
        } else {
            let sql = `insert into homeAddress (buildName, address, price, time, sort, state ) values ("${buildName}","${address}","${price}","${time}","${sort}","${state}") `
            let data = await query(sql)
            if (data.affectedRows === 1) {
                ctx.body = success(data, '添加成功')
            } else {
                ctx.body = error('添加失败')
            }
        }
    }
    //销售模糊查询
    search = async (ctx) => {
        const { shengid, cityid, area } = ctx.request.body
        let sql = `select * from homeAddress where 1=1 `
        let shengSql = `select * from provinces where 1=1 and provinceid=${shengid}`
        let shiSql = `select * from cities where 1=1 and cityid=${cityid}`
        let sheng = await query(shengSql)
        let city = await query(shiSql)

        if (sheng[0].province && city[0].city && area) {
            if (city[0].city === '县' || city[0].city === '市辖区') {
                let opt = sheng[0].province + '-' + area
                sql += `and address like "%${opt}%" `
                let data = await query(sql)
                if (data.length) {
                    ctx.body = success(data, '搜索成功')
                } else {
                    ctx.body = error('没有类似结果')
                }
            } else {
                let opt = sheng[0].province + '-' + city[0].city + '-' + area
                sql += `and address like "%${opt}%" `
                let data = await query(sql)
                if (data.length) {
                    ctx.body = success(data, '搜索成功')
                } else {
                    ctx.body = error('没有类似结果')
                }
            }
        }
    }
    //出租模糊查询
    RentalSearch = async (ctx) => {
        const { area } = ctx.request.body
        let sql = `select * from RentalHousing where 1=1 `
        if (area) {
            sql += `and Villagename like "%${area}%" `
        }
        let data = await query(sql)
        ctx.body = data
    }
    //新增出租
    Rental = async (ctx) => {
        let { Villagename, title, Size, price, sort, state } = ctx.request.body
        let homeSql = `select * from RentalHousing where 1=1  and Villagename like "%${Villagename}%"  `
        let isHave = await query(homeSql)
        if (isHave.length) {
            ctx.body = error('已存在此房源')
        } else {
            let sql = `insert into RentalHousing (Villagename, title, Size, price, sort, state) values ("${Villagename}","${title}","${Size}","${price}","${sort}","${state}") `
            let data = await query(sql)
            if (data.affectedRows === 1) {
                ctx.body = success(data, '添加成功')
            } else {
                ctx.body = error('添加失败')
            }
        }

    }
    //查找出租
    findRental = async (ctx) => {
        let { id } = ctx.request.body
        let sql = `select * from RentalHousing where 1=1 `
        if (id) {
            sql += `and id=${id} `
        }
        let data = await query(sql)
        ctx.body = success(data)
    }
    //出租更新
    changeRentl = async (ctx) => {
        let { id, Villagename, title, Size, price, sort, state } = ctx.request.body
        let sql = `update RentalHousing set Villagename='${Villagename}' , title='${title}',Size='${Size}',price='${price}' ,sort='${sort}', state='${state}' where id=${id} `
        //UPDATE `homeAddress` SET `buildName` = '野马' WHERE `homeAddress`.`id` = 8;
        //UPDATE `homeAddress` SET `buildName` = '清华', `address` = '江苏省-南京市-姑苏', `price` = '80000/平方', `time` = '2020-09-0', `sort` = '6', `state` = '1' WHERE `homeAddress`.`id` = 7;
        let data = await query(sql)
        if (data.affectedRows === 1) {
            ctx.body = success(data, '更新成功')
        } else {
            ctx.body = error('更新失败')
        }
    }
    //删除出租
    deleteRent = async (ctx) => {
        let { id } = ctx.request.body
        let sql = `delete  from RentalHousing where 1=1 and id=${id} `
        let data = await query(sql)
        if (data.affectedRows === 1) {
            ctx.body = success(data, '删除成功')
        } else {
            ctx.body = error('删除失败')
        }
    }
}

module.exports = new Home()